namespace :radiastore do
  desc "Create admin"
  task :create_admin => :environment do
    user = User.find_or_create_by_name(:name => 'Administrator', :email => 'daniel.manteigas@gmail.com', :password => 'zero',
                                    :password_confirmation => 'zero', :login => "admin")
    User.enable_administration user.id
  end
  
  desc "Create main repository to audio assets and Zero's FTP as replica"
  task :create_repositories => :environment do
    replica = ReplicaSystem.find_or_create_by_name(:name => 'Local Storage', :hostname => 'c:/',
                                    :protocol => 'Local', :internal_uri => 'storage/', :main => true)
    replica.save
    
    zero_ftp = ReplicaSystem.find_or_create_by_name(:name => 'FTP Zero', :hostname => 'ftp.radiozero.pt/',
                                    :protocol => 'FTP', :internal_uri => 'upload_de_novos_programas/',
                                    :main => false, :username => 'radiologo', :password => 'radiomix')
  end
  
  desc "Create programs"
  task :create_programs => :environment do
    programs = YAML.load_file(File.dirname(__FILE__) + '/../../config/programs.yml')
    programs.each do |program|
      p = Program.find_or_create_by_name(program)
    end
  end
  
  desc "Create some Audio Assets"
  task :create_assets => :environment do
    assets = YAML.load_file(File.dirname(__FILE__) + '/../../config/audio_assets.yml')
    assets.each do |audio_asset|
      a = AudioAsset.find_or_create_by_name(:name => audio_asset['name'], :description => audio_asset['description'],
            :format => audio_asset['format'], :genre => audio_asset['genre'])
      audio_asset['authors'].each do |author|
        user = User.find_by_login(author)
        User.enable_asset_authorship(user.id, a) if user
      end
    end
  end
end